<template>
  <div id="User">
    <div class>
      <el-button
        size="small"
        type="primary"
        plain
        icon="el-icon-circle-plus"
        @click="toAdd"
        round
        >新 增</el-button
      >
      <span class="widthDiv marginLeft">
        <el-input
          size="small"
          clearable
          v-model="pageParam.username"
          placeholder="请输入内容"
        ></el-input>
      </span>
      <el-button
        size="small"
        type="primary"
        plain
        icon="el-icon-search"
        round
        @click="toSearch"
        >搜 索</el-button
      >
    </div>

    <el-table
      size="mini"
      :data="tableData"
      style="width: 100%"
      :row-class-name="tableRowClassName"
    >
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="165"
      ></el-table-column>
      <el-table-column
        prop="realname"
        label="性名"
        width="165"
      ></el-table-column>
      <el-table-column prop="gender" label="性别" width="150"></el-table-column>
      <el-table-column
        prop="telephone"
        label="手机号"
        width="200"
      ></el-table-column>


      <el-table-column label="出生日期" prop="birth" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{
          moment(scope.row.birth).format("YYYY-MM-DD HH:mm:ss")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini"  type="primary" plain @click="handleEdit(scope.row)" round>编辑</el-button>
          <el-button
            size="small"
            type="danger" 
            plain
            icon="el-icon-delete" circle
            @click="handleDelete(scope.row.id)"
            ></el-button
          >


        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageParam.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageParam.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      :before-close="beforeClose"
    >
      <el-form :model="form" ref="ruleForm" :rules="rules">
        <el-form-item
          prop="username"
          label="用户名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="realname"
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.realname"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="gender" label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="form.gender" autocomplete="off">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          prop="birth"
          label="出生日期"
          :label-width="formLabelWidth"
        >
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.birth"
              style="width: 100%"
              value-format="timestamp"
            ></el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item
          prop="telephone"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.telephone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toCancel">取 消</el-button>
        <el-button type="primary" @click="toSave('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "../utils/request";
let moment = require("moment");
export default {
  data() {
    return {
      form: {},
      title: "录入资讯信息",
      visible: false,
      categoryData: [],
      total: 0,
      tableData: [],
      // 分页查询的参数
      pageParam: {
        page: 1,
        pageSize: 10,
        title: "",
        categoryId: "",
      },
      // 模态框表单控件标题的宽度
      formLabelWidth: "100px",
      //表单校验规则
       rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
        birth: [{ required: true, message: "请选择出生日期", trigger: "blur" }],
        telephone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
    };
  },
  computed: {},
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex %2!== 1) {
        return "success-row";
      }
      //  else {
      //   return "success-row";
      // }
      return "";
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑用户信息";
      this.form = { ...row };
      this.visible = true;
    },
    // 删除
    handleDelete(id) {
      // id 就是要删除的分类的对象的id
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // await等待后面异步函数运行完毕，拿到响应的数据，然后返回
          let res;
          if (id) {
            res = await axios.get("/baseUser/deleteById", { params: { id } });
          }
          this.$notify({
            title: res.status == 200 ? "成功" : "失败",
            message: res.statusText,
            type: res.status == 200 ? "success" : "error",
          });
          this.pageQuery();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    moment,
    // 点击确定按钮
    toSave(formName) {
      this.$refs[formName].validate((valid) => {
        // 校验通过
        if (valid) {
          // console.log(this.form, "=====");
          axios.post("/baseUser/saveOrUpdate", this.form).then((res) => {
            if (res.status == 200) {
              //保存成功，关闭模态框，刷新数据
              this.$notify({
                title: "成功",
                message: "保存成功",
                type: "success",
              });
              this.toCancel();
              this.pageQuery();
            } else {
              this.$notify.error({
                title: "失败",
                message: res.statusText,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 关闭模态框
    toCancel() {
      this.$refs["ruleForm"].resetFields();
      this.visible = false;
    },
    // 模态框关闭之前
    beforeClose(done) {
      this.$refs["ruleForm"].resetFields();
      done();
    },
    // 添加
    toAdd() {
      this.form = {};
      this.visible = true;
      this.title = "录入用户信息";
    },
    // 搜索
    toSearch() {
      this.pageParam.page = 1;
      this.pageQuery();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageParam.pageSize = val;
      this.pageQuery();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageParam.page = val;
      this.pageQuery();
    },
    // 分页查找
    async pageQuery() {
      let res = await axios.get("/baseUser/pageQuery", {
        params: this.pageParam,
      });
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    //查找分类
    async findAllCategory() {
      let res = await axios.get("/baseUser/findAll");
      this.categoryData = res.data;
    },
  },
  created() {
    this.pageQuery();
    this.findAllCategory();
  },
};
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
.widthDiv {
  width: 200px;
  display: inline-block;
  margin-right: 10px;
  margin-bottom: 10px;
}
.marginLeft {
  margin-left: 10px;
}
</style>